package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.pojo.dto.CommodityPageQueryDTO;
import com.heima.pojo.vo.CommodityVO;
import com.heima.mapper.CommodityMapper;
import com.heima.pojo.Commodity;
import com.heima.pojo.dto.CommodityDTO;
import com.heima.pojo.vo.PageSearchQueryVO;
import com.heima.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    CommodityMapper commodityMapper;

/**
 * 新增商品分类
 * **/
    @Override
    public void addSort(CommodityDTO dto) {
        commodityMapper.insert(dto);
    }
    /**
     * 分页查询
     * **/
    @Override
    public PageSearchQueryVO pagingQuery(CommodityPageQueryDTO dto) {
        //获取页码，每页记录数
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        //查询全部数据
       Page<Commodity> page = commodityMapper.pagingQuery(dto);
       //set出Vo中的数据，然后再get DTO中的数据
      PageSearchQueryVO commodityVO = new PageSearchQueryVO();
        commodityVO.setPageIndex(dto.getPageIndex());
        commodityVO.setPageSize(dto.getPageSize());
        commodityVO.setTotalCount((int) page.getTotal());
        commodityVO.setCurrentPageRecords(page.getResult());
      return commodityVO;
    }
/**
 * 根据id查询分类数据
 * **/
    @Override
    public CommodityVO getId(Integer id) {
        CommodityVO vo = commodityMapper.getId(id);
        return vo;
    }
    /**
     * 修改分类数据
     * **/

    @Override
    public void update(String className,Integer classId) {
        commodityMapper.update(className,classId);
    }
/**
 * 根据id删除分类
 * **/
    @Override
    public void deleteId(Integer classId) {
        commodityMapper.deleteId(classId);
    }
}
